package com.cyj.controller;

import com.cyj.dao.AnimeCommentDao;
import com.cyj.entity.AnimeComment;
import com.cyj.entity.AnimeEnshrine;
import com.cyj.entity.FileEntity;
import com.cyj.service.AnimeCommentService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

@Controller
@RequestMapping("/comment")
public class AnimeCommentController {
    @Autowired
    private AnimeCommentService animeCommentService;

    @RequestMapping("/comments")
    public String comments(Locale locale){

        return "animeCommentList";
    }

    @ResponseBody
    @RequestMapping(value = "/animeCommentList",produces = "application/json;charset=utf-8")
    public Map animeComment(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model) throws IOException {
        Map map = new HashMap<>();

        PageInfo page  = animeCommentService.selectAnimeCommentListVo(map,pn,5);

        map.put("pageInfo",page);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/commentBy",produces = "application/json;charset=utf-8")
    public Map enshrineBy(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        Integer commentId = Integer.parseInt(request.getParameter("commentId"));

        map.put("commentId",commentId);

        AnimeComment animeComment = animeCommentService.selectAnimeComment(map);

        System.out.println(animeComment);

        if(animeComment!=null){
            map.put("code", 200);
            map.put("msg", "评论查找成功");
            map.put("data",animeComment);
        }else {
            map.put("code", 100);
            map.put("msg", "评论查找失败");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/updateAnimeComment",produces = "application/json;charset=utf-8")
    public Map updateAnimeComment(HttpServletRequest request, FileEntity entity){

        Map<String, String[]> map = request.getParameterMap();

        AnimeComment animeComment = new AnimeComment();

        try {
            BeanUtils.populate(animeComment,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println(animeComment);

        int num = animeCommentService.updateAnimeComment(animeComment);

        Map<String,Object> map2 = new HashMap<String,Object>();

        if (num>0){
            map2.put("code", 200);
            map2.put("msg", "评论修改成功");
        }else {
            map2.put("code", 100);
            map2.put("msg", "评论修改失败");
        }

        return map2;
    }

    @ResponseBody
    @RequestMapping(value = "/addAnimeComment",produces = "application/json;charset=utf-8")
    public Map addAnimeComment(@RequestBody AnimeComment animeComment){

        Map<String,Object> map = new HashMap<String,Object>();

        animeComment.setCommentTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        System.out.println(animeComment);

        int num = animeCommentService.addAnimeComment(animeComment);


        if(num>0){
            map.put("code",200);
            map.put("msg","评论添加成功！");
        }else{
            map.put("code",100);
            map.put("msg","评论添加失败！");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/delAnimeComment",produces = "application/json;charset=utf-8")
    public Map delAnimeComment(@RequestBody AnimeComment animeComment){

        Map<String,Object> map = new HashMap<String,Object>();

        System.out.println(animeComment);

        int num = animeCommentService.deleteAnimeComment(animeComment.getCommentId());

        if(num>0){
            map.put("code",200);
            map.put("msg","评论删除成功！");
        }else{
            map.put("code",100);
            map.put("msg","评论删除失败！");
        }


        return map;
    }
}
